package com.xiashitech.agent.proxy.hutool.extloadproxy;

import com.xiashitech.agent.cache.classloader.ClassLoaderCache;
import com.xiashitech.agent.config.StartConfig;

public class PooledDSFactoryProxy {

    private static String className = PooledDSFactoryProxy.class.getName().substring(0,PooledDSFactoryProxy.class.getName().length() - 5).replace("extloadproxy","extload");

    private Object pooledDSFactory;

    public static PooledDSFactoryProxy getInstance(Object setting) {
        try {
            Class pooledDSFactoryClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
            PooledDSFactoryProxy pooledDSFactoryProxy = new PooledDSFactoryProxy();
            pooledDSFactoryProxy.pooledDSFactory = pooledDSFactoryClass.getMethod("getInstance", Object.class).invoke(null,setting);
            return pooledDSFactoryProxy;
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
        return null;
    }

    public Object getDataSource() {
        try {
            Class pooledDSFactoryClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
            return pooledDSFactoryClass.getMethod("getDataSource").invoke(pooledDSFactory);
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
        return null;
    }
}
